package net.lab1024.smartadmin.module.weapp.users.controller;

import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.weapp.users.domain.dto.UsersAddDTO;
import net.lab1024.smartadmin.module.weapp.users.domain.dto.UsersUpdateDTO;
import net.lab1024.smartadmin.module.weapp.users.domain.dto.UsersQueryDTO;
import net.lab1024.smartadmin.module.weapp.users.domain.vo.UsersVO;
import net.lab1024.smartadmin.module.weapp.users.domain.vo.UsersExcelVO;
import net.lab1024.smartadmin.module.weapp.users.service.UsersService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * [ 微信小程序 用户信息表 ]
 *
 * @author wuqian
 * @version 1.0
 * @company 五千
 * @copyright (c)  五千Inc. All rights reserved.
 * @date 2021-03-09 14:13:28
 * @since JDK1.8
 */
@RestController
@Api(tags = {"微信小程序 用户信息表"})
public class UsersController extends BaseController {

    @Autowired
    private UsersService usersService;

    @ApiOperation(value = "分页查询微信小程序 用户信息表",notes = "@author wuqian")
    @PostMapping("/users/page/query")
    public ResponseDTO<PageResultDTO<UsersVO>> queryByPage(@RequestBody UsersQueryDTO queryDTO) {
        return usersService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "添加微信小程序 用户信息表",notes = "@author wuqian")
    @PostMapping("/users/add")
    public ResponseDTO<String> add(@RequestBody @Validated UsersAddDTO addTO){
        return usersService.add(addTO);
    }

    @ApiOperation(value="修改微信小程序 用户信息表",notes = "@author wuqian")
    @PostMapping("/users/update")
    public ResponseDTO<String> update(@RequestBody @Validated UsersUpdateDTO updateDTO){
        return usersService.update(updateDTO);
    }

    @ApiOperation(value="批量删除微信小程序 用户信息表",notes = "@author wuqian")
    @PostMapping("/users/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return usersService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author wuqian")
    @PostMapping("/users/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<UsersExcelVO> usersList = usersService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("微信小程序 用户信息表", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, UsersExcelVO.class, usersList);
        downloadExcel("微信小程序 用户信息表", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author wuqian")
    @PostMapping("/users/export/all")
    public void exportAll(@RequestBody @Validated UsersQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<UsersExcelVO> usersList = usersService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("微信小程序 用户信息表", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, UsersExcelVO.class, usersList);
        downloadExcel("微信小程序 用户信息表", workbook, response);
    }

}
